<?php
	require_once 'classes.php';
	
	function loadPage($pagename){
		$tpl = new Template("templates/index.tpl");
		$page = Template::readTemplate($pagename);
		
		$tpl->set('content', $page);
		
		print $tpl->output();
	}
	
	function loadPageContent($pagename){
		$page = Template::readTemplate($pagename);
		
		print $page;
	}
	
	function query($query, $return = false){
		require_once 'config.php';
		
		$db = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
		
		if(mysql_errno()){
			die('Database connecetion failed: ' . mysql_error());
		}		
		
		$link = mysql_select_db(DB_DATABASE);

		if(mysql_errno()){
			die('Database link failed: ' . mysql_error());
		}	
		
		$res = mysql_query($query);

		if(mysql_errno()){
			die('Query failed: ' . mysql_error());
		}	
		
		if($return === true){
			$result = mysql_fetch_array($res);
			mysql_close();
			
			return $result;
		}else{
			mysql_close();
		}
	}	
	
	function clean($str, $encode_ent = false) {
	$str  = @trim($str);
	if($encode_ent) {
		$str = htmlentities($str);
	}
	if(version_compare(phpversion(),'4.3.0') >= 0) {
		if(get_magic_quotes_gpc()) {
			$str = stripslashes($str);
		}
		if(@mysql_ping()) {
			$str = mysql_real_escape_string($str);
		}
		else {
			$str = addslashes($str);
		}
	}
	else {
		if(!get_magic_quotes_gpc()) {
			$str = addslashes($str);
		}
	}
	return $str;
	}
?>